/*
 * Copyright (c) 2020-2022. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.depsea.log.notification;

import lombok.extern.slf4j.Slf4j;
import org.depsea.log.model.RequestInfo;
import org.depsea.log.notification.matcher.IgnoreExceptionMatcher;

/**
 * 抽象异常请求通知
 *
 * @author Wang Chengwei
 * @since 1.0.0 [2022/12/2 14:16]
 */
@Slf4j
public abstract class AbstractExceptionNotification implements ExceptionNotification {

    private IgnoreExceptionMatcher ignoreExceptionMatcher;

    public void setIgnoreExceptionMatcher(IgnoreExceptionMatcher ignoreExceptionMatcher) {
        this.ignoreExceptionMatcher = ignoreExceptionMatcher;
    }

    @Override
    public void doNotify(Exception ex, RequestInfo requestInfo) {
        if (this.ignoreExceptionMatcher != null && this.ignoreExceptionMatcher.matches(ex)) {
            if (log.isDebugEnabled()) {
                log.debug("异常信息为业务异常，已忽略，不进行消息通知。");
            }
            return;
        }

        Message message =  new Message(requestInfo);

        this.sendMessage(message);
    }

    /**
     * 发送消息
     *
     * @param message 消息内容
     */
    protected abstract void sendMessage(Message message);

}
